import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.List;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.*;
public class Parcours extends javax.swing.JFrame{

	private int direction = 0;
	private int startx = 200;
	private int starty = 150;
	private int currentx = 350;
	private int currenty = 150;
	private int previousx = 200;
	private int previousy = 150;
	private Dimension Buttonsize;
	private ArrayList<JScalableLabel> list = new ArrayList<JScalableLabel>();
	private ArrayList<JLabel> Pointlist = new ArrayList<JLabel>();
	private int Circleheight = 5;
	private int Circlewidth = 5;
	private JLayeredPane contentpane = new JLayeredPane();
	private int tiledepth=1;
	private int zoomsteps = 25;
	private float zoomlevel = 0.75f;
	private int zoomdelay = 5;
	private float totalzoom=0.5f;
	private float backgroundzoomx=1f*totalzoom;
	private float backgroundzoomy=0.8f*totalzoom;
	public Parcours() {
		setContentPane(contentpane);
		setLayout(null);
		Insets insets = getInsets();
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		try {
			final BufferedImage bi = ImageIO.read(new File("/home/roel/Afbeeldingen/mindstorms2.jpg"));
			JScalableLabel background = new JScalableLabel(bi,bi,1,1,BarCode.RECHTDOOR,0);
			contentpane.add(background,0,0);
			background.setOpaque(true);
			background.transferFocus();
			background.setScaling(backgroundzoomx, backgroundzoomy);
			Dimension size = background.getPreferredSize();
			background.setBounds(0,0,Math.round(size.width*backgroundzoomx),Math.round(size.height*backgroundzoomy));
			contentpane.validate();
			validate();
			contentpane.repaint();

		} catch (IOException e) {
			System.out.println("error");

		}
//		try {
//			final BufferedImage bi = ImageIO.read(new File("/home/roel/Afbeeldingen/circuitboard.jpg"));
//			drawImage(bi);
//			Dimension size = list.get(list.size()-1).getPreferredSize();
//			Buttonsize = size;
//
//			currentx += size.width;
//
//		} catch (IOException e) {
//			System.out.println("error");
//
//		}
		try {
			final BufferedImage bi = ImageIO.read(new File("/home/roel/Afbeeldingen/forward.jpg"));
			Buttonsize = new Dimension(Math.round(bi.getWidth()*totalzoom),Math.round(bi.getHeight()*totalzoom));

		} catch (IOException e) {
			System.out.println("error");

		}



	}

	public static void main(String args[]){
		Parcours f = new Parcours();
		f.setVisible(true);
		f.setSize(new Dimension(640,400));
		try {
			f.setStartingpoint(5,1);
//			int[] parcours = {0,0,1,2,2,0,1,0,1,1,2,0,2,1,1,0,0,1,2,1,2,2,1,2,1,1,2,1,0,1,2,1,1,2};
			int[] parcours = {0,1,2,0,2,2,1,1,2,2,1,2,2,0,1,1,2,1,2,2,0,1,1,2,1,2,2,0,2,1,2,0,1,2,2,1};
			for(int i=0; i<parcours.length; i++){
				switch(parcours[i]) {
				case 0: f.BarcodeForward();
				break;
				case 1: f.BarcodeLeft();
				break;
				case 2: f.BarcodeRight();
				break;
				}
				Thread.sleep(0);
			}

		}
		catch(Exception E) {
			E.printStackTrace();
		}



	}
	public void setStartingpoint(int i, int j) {
		currentx=Buttonsize.width*i;
		currenty=Buttonsize.height*j;
		System.out.println("startingpoint: " + i + " " + j);
	}

	private void AdjustPosition() {
		previousx=currentx;
		previousy=currenty;
		switch(direction) {
		case 0: currentx+=Buttonsize.width; break;
		case 1: currenty+=Buttonsize.width; break;
		case 2: currentx-=Buttonsize.width; break;
		case 3: currenty-=Buttonsize.width; break;
		}
	}
	public void BarcodeRight() {
        String filename = "/home/roel/barcoderight.mp3";
        MP3 mp3 = new MP3(filename);
        mp3.play();
		try {
			final BufferedImage biRight = ImageIO.read(new File("/home/roel/Afbeeldingen/numbered/right" + direction + ".jpg"));
			final BufferedImage biRightalt = ImageIO.read(new File("/home/roel/Afbeeldingen/numbered/right" + direction + "alt.jpg"));
			drawImage(biRight, biRightalt, BarCode.RECHTS);
			if(direction == 3) {
				direction = 0;
			}
			else {
				direction +=1;	
			}
			AdjustPosition();

		} catch (IOException e) {
			System.out.println("error");

		}

	}
	public void BarcodeLeft() {
        String filename = "/home/roel/barcodeleft.mp3";
        MP3 mp3 = new MP3(filename);
        mp3.play();
		try {
			final BufferedImage bileft = ImageIO.read(new File("/home/roel/Afbeeldingen/numbered/left" + direction + ".jpg"));
			final BufferedImage bileftalt = ImageIO.read(new File("/home/roel/Afbeeldingen/numbered/left" + direction + "alt.jpg"));
			drawImage(bileft, bileftalt,BarCode.LINKS);
			if(direction == 0) {
				direction = 3;
			}
			else {
				direction -=1;	
			}
			AdjustPosition();
		} catch (IOException e) {
			System.out.println("error");

		}

	}
	public void BarcodeForward() {
        String filename = "/home/roel/barcodeforward.mp3";
        MP3 mp3 = new MP3(filename);
        mp3.play();
		try {
			final BufferedImage bi = ImageIO.read(new File("/home/roel/Afbeeldingen/numbered/forward" + direction + ".jpg"));
			final BufferedImage bialt = ImageIO.read(new File("/home/roel/Afbeeldingen/numbered/forward" + direction + "alt.jpg"));
			drawImage(bi, bialt,BarCode.RECHTDOOR);
			AdjustPosition();
		} catch (IOException e) {
			System.out.println("error");

		}

	}
	private void drawImage(final BufferedImage bi, final BufferedImage bialt, BarCode barcode) {
		list.add(new JScalableLabel(bi,bialt,totalzoom,totalzoom, barcode, direction));
		Dimension size = Buttonsize;
		//Zoom out previous JScalableLabel

		if(list.size()>1){
			float localzoomlevel = zoomlevel;
			for(int i=0;i<zoomsteps;i++){
				localzoomlevel = totalzoom+(zoomlevel-totalzoom)/zoomsteps*(zoomsteps-i-1);
				list.get(list.size()-2).setScaling(localzoomlevel,localzoomlevel);
				list.get(list.size()-2).setBounds(previousx-(int)Math.round((localzoomlevel-totalzoom)/2*size.getWidth()/totalzoom), previousy-(int)Math.round((localzoomlevel-totalzoom)/2*size.getHeight()/totalzoom),
						size.width+(int)Math.round((localzoomlevel-totalzoom)*size.getWidth()/totalzoom), size.height+(int)Math.round((localzoomlevel-totalzoom)*size.getHeight()/totalzoom));
//				contentpane.validate();
//				validate();
//				contentpane.repaint();
				try {
					Thread.sleep(zoomdelay);
				} catch (InterruptedException e) {
				}
			}

		}	

		try {
			Thread.sleep(zoomdelay*2);
		} catch (InterruptedException e) {
		}
		//Zoom in on current JScalableLabel
		contentpane.add(list.get(list.size()-1),tiledepth,0);
		list.get(list.size()-1).setOpaque(true);
		list.get(list.size()-1).setlabelLocation(new Point(currentx,currenty));
		list.get(list.size()-1).transferFocus();
			float localzoomlevel = zoomlevel;
			for(int i=0;i<zoomsteps;i++){
				localzoomlevel = totalzoom+(zoomlevel-totalzoom)/zoomsteps*(i+1);
				list.get(list.size()-1).setScaling(localzoomlevel,localzoomlevel);
				list.get(list.size()-1).setBounds(currentx-(int)Math.round((localzoomlevel-totalzoom)/2*size.getWidth()/totalzoom), currenty-(int)Math.round((localzoomlevel-totalzoom)/2*size.getHeight()/totalzoom),
						size.width+(int)Math.round((localzoomlevel-totalzoom)*size.getWidth()/totalzoom), size.height+(int)Math.round((localzoomlevel-totalzoom)*size.getHeight()/totalzoom));
//				contentpane.validate();
//				validate();
//				contentpane.repaint();
				try {
					Thread.sleep(zoomdelay);
				} catch (InterruptedException e) {
				}
			}


//
//		list.get(list.size()-1).setBounds(currentx-(int)Math.round((zoomlevel-1)/2*size.getWidth()), currenty-(int)Math.round((zoomlevel-1)/2*size.getHeight()),
//				size.width+(int)Math.round((zoomlevel-1)*size.getWidth()), size.height+(int)Math.round((zoomlevel-1)*size.getHeight()));
//
//		list.get(list.size()-1).validate();
//		contentpane.validate();
//		validate();
//		contentpane.repaint();
		tiledepth++;
	}



	public ArrayList<JScalableLabel> getList() {
		ArrayList<JScalableLabel> newlist = new ArrayList<JScalableLabel>();
		for(int i=0; i<list.size();i++) {
			newlist.add(list.get(i).clone());
		}
		return newlist;
	}



}


